<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\EmployeeModel;
use app\admin\model\IncomeModel;
use app\admin\model\UserModel;
use app\admin\model\WageCycleModel;
use app\models\Department;
use app\models\DepartmentProduct;
use app\models\Employee;
use app\models\Income;
use app\models\WageModel;
use app\services\JsonService;
use app\services\UtilService as Util;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Exception;

class IncomeController extends AdminBaseController
{
    function index(){
        $department_model   = new Department();
        $departments        = $department_model->getList();
        $this->assign('departments',$departments);
        return $this->fetch();
    }

    public function income_data()
    {
        $page           = $this->request->param('page','1');
        $limit          = $this->request->param('limit','10');
        $keyword        = $this->request->param('keyword','');
        $operation_id   = $this->request->param('operation_id',0);
        $business_id    = $this->request->param('business_id',0);
        $product_id     = $this->request->param('product_id',0);
        $model  = new Income();
        $res    = $model->getDataList($page,$limit,$operation_id,$business_id,$product_id,$keyword);
        $count  = $model->getCount($operation_id,$business_id,$keyword);
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(WageModel::getErrorInfo());
        }
    }

    //营收信息
    public function operate()
    {
        $income_id          = $this->request->param('income_id',0);
        $income_model       = new Income();
        $employee_model     = new Employee();
        $department_model   = new DepartmentProduct();

        if(!empty($income_id)){
            $info           = $income_model->getInfo($income_id);
            $department_id  = $info['department_id'];

            //获取运营、业务列表
            $operations     = $employee_model->getOperationListByDepartmentId($department_id);
            $business       = $employee_model->getBusinessListByDepartmentId($department_id);

            //获取产品
            $products = $department_model->getListByDepartmentId($department_id);
            $info['transfer_time'] = date('Y-m-d H:i:s',$info['transfer_time']);
            $this->assign('products',$products);
            $this->assign('operations',$operations);
            $this->assign('business',$business);
            $this->assign('info',$info);
            $this->assign('department_id',$department_id);
            return $this->fetch('income');
        }else{
            $department_id  = $this->request->param('department_id',0);
            //获取运营、业务列表
            $operations     = $employee_model->getOperationListByDepartmentId($department_id);
            $business       = $employee_model->getBusinessListByDepartmentId($department_id);
            //获取产品
            $products = $department_model->getListByDepartmentId($department_id);
            $this->assign('products',$products);
            $this->assign('operations',$operations);
            $this->assign('business',$business);
            $this->assign('department_id',$department_id);
            return $this->fetch('add');
        }
    }



    public function do_operate()
    {
        $data = Util::postMore([
            ['income_id', 0],
            'money',
            'department_id',
            'company',
            'type',
            ['operation_id',0],
            ['business_id',0],
            'remark',
        ]);
        $incomeModel = new Income();
        if($data['income_id']){
            $re = $incomeModel->edit($data['income_id'],$data);
        }else{
            $re = $incomeModel->create($data);
        }
        if($re){
            JsonService::success('操作成功');
        }else{
            JsonService::fail('操作失败');
        }
    }


}
